package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ActivitiesView extends Activity{
	
	public Calendar date;
	private String[][] aboutActivities = null;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activities);
	    
	    updateListValues();
	    ListView activities = (ListView)findViewById(R.id.listViewActivities);
	    activities.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,
	    		int position, long id) {
	    		showActivities(aboutActivities[position][0], aboutActivities[position][1],
	    				aboutActivities[position][2], aboutActivities[position][3]);
	    	}
	    });
	    
	    
	}

//	protected void editActivityName(final String date, final String name, final String description) {
//		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
//		 helpBuilder.setTitle("Editando nome da atividade: " + name);
//		 helpBuilder.setMessage("Digite o nome da atividade: ");
//		 final EditText input = new EditText(this);
//		 input.setSingleLine();
//		 input.setText(name);
//		 helpBuilder.setView(input);
//	
//		 helpBuilder.setPositiveButton("Proximo", new DialogInterface.OnClickListener() {
//			  @Override
//			  public void onClick(DialogInterface dialog, int which) {
//				  ActivityName = "";
//				  ActivityName = input.getText().toString();
//				  editActivityDescription(name, description);
//				  dialog.cancel();
//			  }
//		 });
//		 helpBuilder.setNeutralButton("Cancelar", new DialogInterface.OnClickListener() {
//		  @Override
//		  public void onClick(DialogInterface dialog, int which) {
//			  dialog.cancel();
//		  }
//		 });
//		
//		 AlertDialog helpDialog = helpBuilder.create();
//		 helpDialog.show();		
//	}
//		
	private void updateListValues() {
	    ListView activities = (ListView)findViewById(R.id.listViewActivities);

	    BD bd = new BD();
	    ArrayList<String> activi = bd.getActivityOrderByDate(this);
	    for (int i = 0; i < activi.size(); i++) {
			System.out.println(activi.get(i));
		}
	    
		aboutActivities = new String[activi.size()][4];
	    
	    for (int i = 0; i < activi.size(); i++) {
			String[] date = activi.get(i).split(" ");
			aboutActivities[i][0] = date[0];
			aboutActivities[i][1] = date[1];
			aboutActivities[i][2] = date[2];
			aboutActivities[i][3] = date[3];
		}
	    
	    String[] values = new String[aboutActivities.length];
	    for (int i = 0; i < values.length; i++) {
			values[i] = aboutActivities[i][0] + " - " +  aboutActivities[i][2];
		}
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    activities.setAdapter(adapter);
	    activities.invalidate();
	}
	
//	protected void editActivityDescription(final String name, final String description) {
//		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
//		 helpBuilder.setTitle("Editando atividade: " + name);
//		 helpBuilder.setMessage("Digite a nova descricao: ");
//		 final EditText input = new EditText(this);
//		 input.setSingleLine();
//		 input.setText(description);
//		 helpBuilder.setView(input);
//	
//		 helpBuilder.setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
//			  @Override
//			  public void onClick(DialogInterface dialog, int which) {
//				  ActivityDescription = "";
//				  ActivityDescription = input.getText().toString();
//				  BD bd = new BD();
//				  bd.editActivity(ActivitiesView.this, name ,ActivityName, ActivityDescription);
//				  updateListValues();
//				  dialog.cancel();
//			  }
//		 });
//		 helpBuilder.setNeutralButton("Cancelar", new DialogInterface.OnClickListener() {
//		  @Override
//		  public void onClick(DialogInterface dialog, int which) {
//			  dialog.cancel();
//		  }
//		 });
//		
//		 AlertDialog helpDialog = helpBuilder.create();
//		 helpDialog.show();
//		
//	}

	
	private void showActivities(final String date, final String hour, final String name, final String description) {
		 final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(name);
		 helpBuilder.setMessage("Date:" + date + "\nHour: " + hour +   "\nName: " + name + "\nDescription: " + description);
		 helpBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
//				  editActivityName(); //date, name, description
				  dialog.cancel();
			  }
		 });
		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
			 });
		 helpBuilder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  BD bd = new BD();
				  bd.removeActivity(ActivitiesView.this, name);
				  dialog.cancel();
				  startActivity(getIntent()); 
				  finish();
			  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		}
	
//	@Override
//	public void onBackPressed() {
//		
//        back();	        		 
//        return;
//	}
//	
//	private void back() {
//
//		int month = date.get(Calendar.MONTH);
//        int year = date.get(Calendar.YEAR);
//        int today = date.get(Calendar.DATE);
//        
//    	Intent trocatela = new Intent(ActivitiesView.this, CalendarView.class);
//    	trocatela.putExtra("date", year+"-"+month+"-"+today);
//    	
//    	ActivitiesView.this.startActivity(trocatela);
//    	ActivitiesView.this.finish();	 
//	}
	
}
